package com.macrohard.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macrohard.admin.service.InformationService;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.Information;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("information")
@Validated
@Api(tags = "资讯接口")
@RequiredArgsConstructor
public class InformationController {
	private final InformationService informationService;

	@GetMapping
	@ApiOperation("分页获取所有资讯")
	public R get(@NotNull Integer current, @NotNull Integer pageSize) {
		Page<Information> page = new Page<>(current, pageSize);
		LambdaQueryWrapper<Information> queryWrapper = new LambdaQueryWrapper<>();
		page = informationService.page(page, queryWrapper);
		return R.ok("ok", page);
	}

	@ApiOperation("根据id获取资讯")
	@GetMapping("{id}")
	public R get(@PathVariable Integer id) {
		return R.ok("ok", informationService.getById(id));
	}

	@ApiOperation("添加资讯")
	@PostMapping
	public R add(@RequestBody Information information) {
		return R.wrapEffectResult(informationService.save(information));
	}

	@PutMapping
	@ApiOperation("修改资讯")
	public R update(@RequestBody Information information) {
		return R.wrapEffectResult(informationService.updateById(information));
	}

	@ApiOperation("根据id删除资讯")
	@DeleteMapping("{id}")
	public R delete(@PathVariable Integer id) {
		return R.wrapEffectResult(informationService.removeById(id));
	}

	@ApiOperation("批量删除资讯")
	@DeleteMapping
	public R delete(@RequestBody List<Integer> ids) {
		return R.wrapEffectResult(informationService.removeByIds(ids));
	}

}
